package oa.hleast.xswl.service.place.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import oa.hleast.xswl.mapper.place.PlaceInfoAttributesMapper;
import oa.hleast.xswl.pojo.domain.place.CmsPlaceInfoAttributes;
import oa.hleast.xswl.pojo.form.place.PlaceInfoAttributesForm;
import oa.hleast.xswl.pojo.vo.place.CmsPlaceInfoAttributesVo;
import oa.hleast.xswl.service.place.IPlaceInfoAttributesService;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author 302
 * @description
 * @date 2021/3/20
 */

@Slf4j
@Service
@AllArgsConstructor
public class PlaceInfoAttributesServiceImpl extends ServiceImpl<PlaceInfoAttributesMapper, CmsPlaceInfoAttributes> implements IPlaceInfoAttributesService {


    @Override
    public List<CmsPlaceInfoAttributesVo> getByPlaceId(Long placeTypeId) {

        LambdaQueryWrapper<CmsPlaceInfoAttributes> queryWrapper = new LambdaQueryWrapper<CmsPlaceInfoAttributes>()
                .eq(CmsPlaceInfoAttributes::getPlaceTypeId, placeTypeId);
        List<CmsPlaceInfoAttributes> list = this.list(queryWrapper);
        return conversionVoList(list);
    }

    @Override
    public Boolean saveAttributesList(List<PlaceInfoAttributesForm> formList) {

        Long placeTypeId = formList.get(0).getPlaceTypeId();
        List<Long> formIds = formList.stream().map(item -> item.getId()).collect(Collectors.toList());
        List<Long> databaseIds = this.
                list(new LambdaQueryWrapper<CmsPlaceInfoAttributes>()
                        .eq(CmsPlaceInfoAttributes::getPlaceTypeId, placeTypeId)
                        .select(CmsPlaceInfoAttributes::getId)
                ).stream()
                .map(item -> item.getId())
                .collect(Collectors.toList());

        // 删除的商品分类属性
        if (CollectionUtil.isNotEmpty(databaseIds)) {
            List<Long> removeIds = databaseIds.stream()
                    //过滤
                    .filter(id -> CollectionUtil.isEmpty(formIds) || !formIds.contains(id))
                    .collect(Collectors.toList());
            if (CollectionUtil.isNotEmpty(removeIds)) {
                this.removeByIds(removeIds);
            }
        }

        List<CmsPlaceInfoAttributes> attributesList = conversionToPojo(formList);

        boolean save = this.saveOrUpdateBatch(attributesList);
        log.info("formList ={}, attributesList = {},save = {}", formList, attributesList, save);

        return save;
    }


    /**
     * ===========  私有方法  ===========
     */


    private List<CmsPlaceInfoAttributesVo> conversionVoList(List<CmsPlaceInfoAttributes> list) {
        return list.stream().map(this::conversionVo).collect(Collectors.toList());
    }

    private CmsPlaceInfoAttributesVo conversionVo(CmsPlaceInfoAttributes placeInfoAttributes) {
        CmsPlaceInfoAttributesVo vo = new CmsPlaceInfoAttributesVo();
        BeanUtil.copyProperties(placeInfoAttributes, vo);
        return vo;
    }

    private List<CmsPlaceInfoAttributes> conversionToPojo(List<PlaceInfoAttributesForm> formList) {
        if (CollectionUtils.isEmpty(formList)) {
            return new ArrayList<>();
        }
        return formList.stream().map(this::conversionToPojo).collect(Collectors.toList());
    }

    private CmsPlaceInfoAttributes conversionToPojo(PlaceInfoAttributesForm form) {
        CmsPlaceInfoAttributes attributes = new CmsPlaceInfoAttributes();
        BeanUtil.copyProperties(form, attributes);
        return attributes;
    }
}
